package com.daiyang.javabasics.serializationanddeserialization;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SerializationAndDeserialization {
    public static void main(String[] args) {
        User user = new User("daiyang", 18);
        try {
            ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get("user.ser")));
            oos.writeObject(user);
            System.out.println("serialized user");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get("user.ser")));
            User deserializedUser = (User) ois.readObject();
            System.out.println("deserialized user:"+deserializedUser);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }
}
